<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {height:34px;line-height:34px;padding:0 8px;}
</style>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div >
            <table id="menu-table" class="layui-table" lay-filter="menu-table"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
{{--        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>--}}
    </div>
</script>
<!-- 状态列 -->
<script type="text/html" id="status">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="lock" value="@{{d.id}}" title="显示" lay-filter="lockDemo" @{{ d.status === 1  ? 'checked' : '' }}>
</script>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var treetable = layui.treetable;
        var miniPage = layui.miniPage;

        // 渲染表格
        layer.load(2);
        var menu_table = treetable.render({
            toolbar: '#toolbarDemo',
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#menu-table',
            url: 'menu/list',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 200, title: '名称'},
                {field: 'href', title: '菜单url'},
                {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.is_menu === 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.pid !== 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {field:'status',title:'显示', align:'center',templet:'#status',unresize:true},
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#menu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#menu-table');
        });

        form.on('checkbox(lockDemo)',function (obj) {
            $.ajax({
                url:'menu_change_status/'+ this.value,
                method: 'post',
                success: function (res) {
                    layer.msg(res.msg)
                }
            })
        })

        //监听工具条
        table.on('tool(menu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('真的删除这个菜单吗', function (index) {
                    $.ajax({
                        url:'menu/'+data.id,
                        method: 'DELETE',
                        success: function (res) {
                            layer.msg(res.msg,function () {
                                obj.del();
                                layer.close(index);
                            })
                        }
                    })
                });
            } else if (layEvent === 'edit') {
                // layer.msg('修改' + data.id);
                var content = miniPage.getHrefContent('menu/'+data.id+'/edit');
                var openWH = miniPage.getOpenWidthHeight();
                var edit_view = layer.open({
                    title: '修改菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(edit_view);
                });
            }
        });
        /**
         * toolbar事件监听
         */
        table.on('toolbar(menu-table)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('menu/create');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });
    });
</script>
</body>
